# -*- encoding: utf-8 -*-
"""
@File    : User_loginout_try.py
@Time    : 2020/3/20 7:44 下午
@Author  : david wang
@Email   : 3063737@qq.com
@Software: PyCharm
"""
# 导入插件
import pandas as pd
import numpy as np
from pandas import DataFrame
import setting

# 定义文件路径
filename = setting.export_file+'User_loginout_try.html'
iopath = setting.resource_file+'User_order.xlsx'
outpath = setting.export_file+'out.png'

# 读取xls文件
df = pd.DataFrame(pd.read_excel(iopath, sheet_name='Sheet1'))

# 获取数据
cols = df.columns.values.tolist()  # 获取数据头信息
da0 = [str(i)[0:10] for i in pd.Series.tolist(pd.to_datetime(df[cols[0]]))]
# da1 = pd.Series.tolist(df[cols[1]])
da2 = pd.Series.tolist(df[cols[2]])
da3 = pd.Series.tolist(df[cols[3]])

# 导入柱状图-Bar
from pyecharts.charts import Line, Bar, Grid
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot


def bar_base() -> Line:
    c = (
        Line()
            .add_xaxis(da0)
            .add_yaxis(cols[2], (da2), label_opts=opts.LabelOpts(is_show=True))
            .add_yaxis(cols[3], (da3), yaxis_index=0, label_opts=opts.LabelOpts(is_show=True))
            .set_global_opts(title_opts=opts.TitleOpts(title="遏制羊毛党计划-注销", subtitle="试用用户量和注销用户量"),
                             legend_opts=opts.LegendOpts(pos_left="40%"),datazoom_opts=[opts.DataZoomOpts()
                              # , opts.DataZoomOpts(orient="vertical")
                                                                                        ],
                             )
            .extend_axis(
            yaxis=opts.AxisOpts(name="试用数/个", position='right'))

            .extend_axis(
            yaxis=opts.AxisOpts(name="注销数/个", type_="value", position="left",  # 设置y轴的名称，类型，位置
                                # axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color="#483D8B")),
                               )

        )
    )

    bar = (
        Bar()
            .add_xaxis(da0)
            .add_yaxis(cols[2], (da2), label_opts=opts.LabelOpts(is_show=False), yaxis_index=2,
                       # category_gap设置间距
                       # category_gap="1%",
                       # is_selectedFalse默认不显示某商品
                       is_selected=True,
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                       markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
                                                               opts.MarkPointItem(type_="min", name="最大值"),
                                                               opts.MarkPointItem(type_="average", name="平均值"), ]))
            .add_yaxis(cols[3], (da3), label_opts=opts.LabelOpts(is_show=False, position="inside"),
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                       markpoint_opts=opts.MarkPointOpts(
                           data=[
                                 # opts.MarkPointItem(type_="min", name="最小值"),
                                 opts.MarkPointItem(type_="max", name="最大值"),
                                 opts.MarkPointItem(type_="average", name="平均值"), ]))

    )

    c.overlap(bar)
    return Grid(init_opts=opts.InitOpts(theme=ThemeType.WHITE,
                                        height="700px", width="1500px"
                                        )).add(c, opts.GridOpts(pos_left="5%", pos_top='20%'), is_control_axis_index=True, )

bar_base().render(filename)

import webbrowser
webbrowser.open_new_tab( 'file://'+filename)

